//
//  StatusCell.m
//
//  Created by Olof Millberg on 2010-04-06.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "StatusCell.h"

@implementation StatusCell

@synthesize statusButton;
@synthesize priorityLabel;
@synthesize statuses;
@synthesize delegate;
@synthesize workOrder;

- (void) dealloc
{
	[statusButton release];
	[priorityLabel release];
	[statuses release];
	[super dealloc];
}

- (void) statusChanged:(NSString *)status
{
	self.workOrder.status = status;
}

- (void)populateWithDataFromWorkOrder:(WorkOrder *)aWorkOrder
{
	self.workOrder = aWorkOrder;
	self.priorityLabel.text = aWorkOrder.priority;
	[self.statusButton setTitle:aWorkOrder.status forState:UIControlStateNormal];
	self.delegate = self;
	NSRange range; range.location = 1; range.length = aWorkOrder.statusTypes.count - 2;
	self.statuses = (NSMutableArray*)[aWorkOrder.statusTypes subarrayWithRange:range];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if (buttonIndex != actionSheet.cancelButtonIndex)
	{
		[self.statusButton setTitle:[actionSheet buttonTitleAtIndex:buttonIndex] forState:UIControlStateNormal];
		[delegate statusChanged:self.statusButton.currentTitle];
	}
	[actionSheet release];
}

- (void) showStatusPicker
{
	UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Sätt status"
													  delegate:self
											 cancelButtonTitle:nil
										destructiveButtonTitle:nil
											 otherButtonTitles:nil];
	
	for (NSString *status in statuses)
		[actionSheet addButtonWithTitle:status];
	
	[actionSheet addButtonWithTitle:@"Tillbaka"];
	[actionSheet setCancelButtonIndex:statuses.count];
	[actionSheet showInView:self.window];
}

@end